﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_DiscussAreaSetting_DiscussSettingIndex : System.Web.UI.Page
{
    private static PagedDataSource pds = new PagedDataSource();
    private static int da_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString.Get("da_id") != null)
            {
                da_id = int.Parse(Request.QueryString.Get("da_id"));
                pds.AllowPaging = true;
                pds.PageSize = 20;
                this.DataList_DataTable();
                this.DataList_DataBind();
            }
        }
    }

    private void DataList_DataTable()
    {
        DiscussThemetbService dtService = new DiscussThemetbService();
        DiscussThemeCommentstbService dtcService = new DiscussThemeCommentstbService();
        DataTable dt = new DataTable();
        dt.Columns.Add("DiscussThemeId", typeof(string));
        dt.Columns.Add("DiscussThemeTitle", typeof(string));
        dt.Columns.Add("DiscussThemeAuthor", typeof(string));
        dt.Columns.Add("DiscussThemeCreateTime", typeof(string));
        dt.Columns.Add("DiscussThemeHot", typeof(string));
        dt.Columns.Add("DiscussThemeAppCount", typeof(string));
        dt.Columns.Add("DiscussThemeLastAppTime", typeof(string));
        dt.Columns.Add("DiscussThemeLastAppUser", typeof(string));
        List<DiscussThemetb> list = dtService.findALLByDaId(da_id);
        if (list != null && list.Count > 0)
        {
            foreach (DiscussThemetb discussTheme in list)
            {
                DataRow dr = dt.NewRow();
                dr["DiscussThemeId"] = discussTheme.DiscussThemeId.ToString();
                dr["DiscussThemeTitle"] = discussTheme.DiscussThemeTitle;
                dr["DiscussThemeAuthor"] = discussTheme.DiscussThemeAuthor.UserName;
                dr["DiscussThemeCreateTime"] = discussTheme.DiscussThemeCreateTime.ToString();
                dr["DiscussThemeHot"] = discussTheme.DiscussThemeHot.ToString();
                dr["DiscussThemeAppCount"] = discussTheme.DiscussAppCount.ToString();
                DiscussThemeCommentstb dtcTb = dtcService.FindTheOneByLastReadTime(discussTheme.DiscussThemeId);
                if (dtcTb != null)
                {
                    dr["DiscussThemeLastAppTime"] = dtcTb.DtCommentsCreateTime.ToString();
                    dr["DiscussThemeLastAppUser"] = dtcTb.DtCommentsAuthor.UserName;
                }
                dt.Rows.Add(dr);
            }
            pds.DataSource = dt.DefaultView;
        }
        else
        {
            pds.DataSource = null;
        }
    }

    private void DataList_DataBind()
    {
        if (pds.DataSource == null)
        {
            this.DataList1.DataSource = null;
        }
        else
        {
            this.DataList1.DataSource = pds;
        }
        this.DataList1.DataBind();
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        pds.CurrentPageIndex--;
        this.DataList_DataBind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        pds.CurrentPageIndex++;
        this.DataList_DataBind();
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string dtid = ((HtmlInputHidden)e.Item.FindControl("HidDtId")).Value;
            ((Button)e.Item.FindControl("IbtDel")).Attributes.Add("dt_id", dtid);
            ((Button)e.Item.FindControl("IbtDel")).Attributes.Add("onclick", "javascript:return confirm('您确认删除此讨论主题吗？')");
            //((HyperLink)e.Item.FindControl("HylTitle")).NavigateUrl = "~/DiscussArea/ThemeAppear.aspx?dt_id=" + dtid;
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (pds.IsFirstPage)
            {
                ((ImageButton)e.Item.FindControl("ImageButton3")).Enabled = false;
            }
            if (pds.IsLastPage)
            {
                ((ImageButton)e.Item.FindControl("ImageButton2")).Enabled = false;
            }
        }
    }
    protected void IbtDel_Click(object sender, EventArgs e)
    {
        int ci = pds.CurrentPageIndex;
        int dtid = int.Parse(((ImageButton)sender).Attributes["dt_id"]);
        DiscussThemetbService dtService = new DiscussThemetbService();
        dtService.DelThemeById(dtid);
        this.DataList_DataTable();
        if (ci <= pds.PageCount - 1)
        {
            pds.CurrentPageIndex = ci;
        }
        else
        {
            pds.CurrentPageIndex = pds.PageCount - 1;
        }
        this.DataList_DataBind();
    }
}
